package com.tkm.builtinTypes.ranges

fun main() {
    //  IntRange、CharRange、LongRange（离散型区间）
    val intRange = 1..10
    val charRange = 'a'..'z'
    val longRange = 1L..100L
    //  ClosedRange<String>
    val stringRange = "a".."z"
    println(intRange.joinToString())

    //  连续型区间（不可数，无法调用joinToString方法）
    val doubleRange = 1.0..10.0
    val floatRange = 1.0f..10.0f

    //  step：步长
    val intRangeWithStep = 1..10 step 2
    val charRangeWithStep = 'a'..'z' step 2
    val longRangeWithStep = 1L..100L step 2
    println(intRangeWithStep.joinToString())

    //  半闭半开区间
    val intRangeExclusive = 1 until 10
    val charRangeExclusive = 'a' until  'z'
    val longRangeExclusive = 1L until 100L
    println(intRangeExclusive.joinToString())

    //  倒序区间
    val intRangeReverse = 10 downTo 1
    val charRangeReverse = 'z' downTo 'a'
    val longRangeReverse = 100L downTo 1L
    println(intRangeReverse.joinToString())

    //  离散区间是可以遍历的，也可以判断元素是否在其中
    intRange.forEach { println(it) }
    charRangeReverse.forEach { println(it) }
    println(1 in intRange)
    //  连续区间不能遍历，只能判断元素是否在其中
    println(3.0 in doubleRange)
    println(-1f !in floatRange)

    //  Kotlin中for-i遍历，利用until
    val array = intArrayOf(1, 2, 3, 4, 5)
    for (i in 0 until array.size) {
        val el = array[i]
        println(el)
    }
    //  indice：半闭半开区间，相当于until，返回IntArray
    for (i in array.indices) {
        val el = array[i]
        println(el)
    }
}
